gusucode.com > VC 封包截取的源代码-源码程序 > VC 封包截取的源代码-源码程序/code/S3DHook/S3DHook.cpp

    //Download by http://www.NewXing.com
// S3DHook.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#define S3DHOOKAPI extern "C" __declspec(dllexport)
#include "S3DHook.h"
#include "madCHook.h"
#include "Winsock2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma comment(linker,"section:Shared,rws")
#pragma data_seg("Shared")
HHOOK g_hhook;
#pragma data_seg()


//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CS3DHookApp

BEGIN_MESSAGE_MAP(CS3DHookApp, CWinApp)
	//{{AFX_MSG_MAP(CS3DHookApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CS3DHookApp construction

CS3DHookApp::CS3DHookApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CS3DHookApp object

CS3DHookApp theApp;
CS3DHookDlg *pCWndWGMain;

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//按F12弹起时呼出外挂
BOOL bKeyUp = lParam & (1 << 31);
if (bKeyUp && wParam == VK_F12 && nCode == HC_ACTION) {
if (pCWndWGMain == NULL) 
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CWnd *pCWnd = CWnd::GetForegroundWindow();
//当前窗口是否为记事本窗口
char buf[MAX_PATH];
::GetClassName(pCWnd->GetSafeHwnd(), buf, MAX_PATH);
if (lstrcmpi(buf, "ZoneLobbyWindow") == 0) {
pCWndWGMain = new CS3DHookDlg();
//创建"外挂呼出窗口"时把记事本窗口作为他的父窗口
pCWndWGMain->Create(IDD_S3DHOOK_DIALOG, pCWnd);
pCWndWGMain->ShowWindow(SW_SHOW);
}
}
else 
{
//根据当前呼出窗口的状态来显示或隐藏呼出窗口
pCWndWGMain->ShowWindow(pCWndWGMain->IsWindowVisible() ? SW_HIDE : SW_SHOW);
}
}
return ::CallNextHookEx(g_hhook, nCode, wParam ,lParam);
}


S3DHOOKAPI BOOL WINAPI InstallHook()
{
if (g_hhook == NULL) {
g_hhook = ::SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, theApp.m_hInstance, 0);
if (g_hhook != NULL)
return TRUE;
}
return FALSE;
}

S3DHOOKAPI BOOL WINAPI UninstallHook()
{
return ::UnhookWindowsHookEx(g_hhook);
}

/////////////////////////////////////////////////////////////////////////////
// CS3DHookDlg dialog


CS3DHookDlg::CS3DHookDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CS3DHookDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CS3DHookDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CS3DHookDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CS3DHookDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CS3DHookDlg, CDialog)
	//{{AFX_MSG_MAP(CS3DHookDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CS3DHookDlg message handlers

int CS3DHookApp::ExitInstance() 
{

// TODO: Add your specialized code here and/or call the base class
    delete pCWndWGMain;
    return CWinApp::ExitInstance();
	
}

int (WINAPI *oWSASend)(
SOCKET,
LPWSABUF,
DWORD,
LPDWORD,
DWORD,
LPWSAOVERLAPPED,
LPWSAOVERLAPPED_COMPLETION_ROUTINE
);
int cWSASend(
SOCKET s,
LPWSABUF lpBuffers,
DWORD dwBufferCount,
LPDWORD lpNumberOfBytesSent,
DWORD dwFlags,
LPWSAOVERLAPPED lpOverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);


BOOL CS3DHookDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	HookAPI("Ws2_32.dll", "WSASend", cWSASend, (PVOID *) &oWSASend);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

int cWSASend(SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
 LPDWORD lpNumberOfBytesSent, DWORD dwFlags,
 LPWSAOVERLAPPED lpOverlapped,
 LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
{
char buf[1024];
lstrcpyn(buf,lpBuffers->buf,lpBuffers->len);
CListBox *pListBox = (CListBox *)pCWndWGMain->GetDlgItem(IDC_LIST_SEND);
pListBox->AddString(buf);
                //API钩子返回之前,对原始的API进行调用,
return oWSASend(s, lpBuffers, dwBufferCount, lpNumberOfBytesSent, dwFlags, lpOverlapped, lpCompletionRoutine);
}